<template>
  <basic-container >
    <div style="width: 100%;height: 100%;display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;">
      <div style="font-size: 14px;margin-top: 30px;height: 46px;display: flex;align-items: center;
background: #FFF7E8;width: 98%">
          <img src="https://yulonghuyu.oss-cn-hangzhou.aliyuncs.com/app-web/xitong/zhuyidapx%402x.png" style="width: 18px;height: 18px;margin-left: 30px">
        <span style="margin-left: 20px">
        说明：平台优惠券是<span style="color: red">平台承担部分金额</span>的一种优惠券类型，<span style="color: red">平台参与法人活动请使用平台优惠券</span>，请和运营方认真核对无误后使用。
          </span>
      </div>
      <el-form  :model="form" label-width="120px" :rules="rules" style="margin-top: 20px" ref="ruleForm">
        <el-form-item label="游戏：" >
          <el-select style="width: 500px" v-model="form.gameProjectId" filterable clearable placeholder="请选择" @click.native="gameSelect">
            <el-option
              v-for="item in gamelist"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item prop="account" label="玩家账号：">
          <el-input style="width: 500px" type="textarea" clearable v-model="form.account"></el-input>
          <span style="color: red;margin-left: 10px">注：一行一个账号</span>
        </el-form-item>
        <el-form-item label="优惠档位：" prop="id">
          <el-select style="width: 500px" v-model="form.id" @click.native="coupon" clearable placeholder="请选择" >
            <el-option
              v-for="item in couponList"
              :key="item.activityCouponId"
              :label="item.couponName+'(活动时间'+formatDateTime(item.activityStartTime) +' 到 '+ formatDateTime(item.activityEndTime)+')'"
              :value="item.activityCouponId" @click.native="optionCoupon(item.activityCouponId)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="可开放时间：">
          <div v-if="showTime">开始：{{startTime}}</div>
          <div v-if="showTime">结束：{{endTime}}</div>
        </el-form-item>
        <el-form-item label="活动有效时间：">
          <div v-if="showTime">开始：{{activityStartTime}}</div>
          <div v-if="showTime">结束：{{activityEndTime}}</div>
        </el-form-item>
        <el-form-item label="可领取次数：">
          <div v-if="showTime">1次</div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="closePayPackageSure('ruleForm')">确认发放</el-button>
          <el-button  @click="form={}">取消</el-button>
        </el-form-item>
      </el-form>

    </div>
  </basic-container>
</template>

<script>


import { gameList} from "@/api/game/game";
import {detailCouponList,grantCoupon} from "@/api/system/details";

export default {
  data() {
    return {
      form: {
        gameProjectId: '',
        account: '',
        id: '',
        type:2,
        sendType:1,//这个页面的发送方式
        activeName:'1'
      },
      showTime:false,
      gamePra:{ gameProjectId:''},
      gamelist:[],
      couponList:[],
      startTime: '',
      endTime: '',
      activityStartTime: '',
      activityEndTime: '',
      rules: {
        account: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        id: [
          {required: true, message: '请选择', trigger: 'blur'}
        ],
      },
    }

  },

  computed: {

  },
  created() {
    this.game();
  },
  methods: {
    gameSelect(){
      this.$set(this.form,'id','')
      this.showTime=false
      this.startTime= ''
      this.endTime= ''
      this.activityStartTime= ''
      this.activityEndTime= ''
      this.couponList=[]
    },
    coupon(){
      if(this.form.gameProjectId==''){
        this.$message({
          type: "error",
          message: "请先选择游戏!"
        });
        return;
      }
      this.showTime=false
      this.startTime= ''
      this.endTime= ''
      this.activityStartTime= ''
      this.activityEndTime= ''
      this.gamePra.gameProjectId=this.form.gameProjectId
      detailCouponList(this.gamePra).then(res => {
        const data = res.data.data;
        console.log("data",data)
        this.couponList = data;
      });
    },
    formatDateTime(timeStamp){
      var date = new Date();
      date.setTime(timeStamp);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      return y + '-' + m + '-' + d+' ';
    },
    isNotEmpty(arr) {
      return arr != null && arr.length !== 0
    },
    isEmpty(arr) {
      return !this.isNotEmpty(arr)
    },
    game(){
      gameList().then(res => {
        const data = res.data.data;
        console.log("data",data)
        this.gamelist = data;
      });
    },
    optionCoupon(val){
      console.log("val",val)
      for (let i = 0; i < this.couponList.length; i++) {
        if(this.couponList[i].activityCouponId==val){
          this.startTime=this.formatDateTime(this.couponList[i].startTime)
          this.endTime=this.formatDateTime(this.couponList[i].endTime)
          this.activityStartTime=this.formatDateTime(this.couponList[i].activityStartTime)
          this.activityEndTime=this.formatDateTime(this.couponList[i].activityEndTime)
        }
      }
      this.showTime=true
    },

    closePayPackageSure(ruleForm){
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.$confirm("确定发放这些玩家优惠券？", '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            grantCoupon(this.form).then(res=>{
              let data = res.data;
              console.log("zs", data)
              if(data.code == 200) {
                this.$message({
                  message: '成功赠送优惠券！',
                  type: 'success'
                });
                this.form.gameProjectId = ''
                this.form.account = ''
                this.form.id = ''
              } else {
                this.$alert(data.msg.replace(/\n/g, "") , '', {
                  confirmButtonText: '确定',
                  callback: action => {
                    this.form.gameProjectId = ''
                    this.form.account = ''
                    this.form.id = ''
                  }
                });
              }
            })
          }).catch(() => {
          });
        } else {
          console.log('error submit!!')
          return false
        }
      })

    },
  }
}
</script>

<style scoped>

</style>
